package com.ruoyi.web.controller.netty.stickyPackage;

import cn.hutool.core.util.CharsetUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class StickPackageServerHandler extends ChannelInboundHandlerAdapter {
    private AtomicInteger counter = new AtomicInteger(0);

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf)msg;
        String request = byteBuf.toString(CharsetUtil.charset(CharsetUtil.UTF_8));
        log.info("服务端接收到请求数量为" + counter.incrementAndGet());
        String resp = request + "成功请求";
        ctx.writeAndFlush(Unpooled.copiedBuffer(resp.getBytes()));
    }
}
